﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace LinkED_WP
{
    public partial class TouchPad : PhoneApplicationPage
    {
        static private SreenPos m_msg;
        static private LinkED_UDP m_UDP;
        static private LinkED_TCP m_TCP;
        static private int m_ServerPort = 0;
        static private string m_ServerIP = "";
        static private bool m_bTCP = false;
        private int m_CurrentFigerNumer = 0;
        private double m_ScrollX = 0;
        private double m_ScrollY = 0;
        private Point m_PreviousPoint;

        public TouchPad()
        {
            InitializeComponent();
            m_UDP = new LinkED_UDP();
            m_TCP = new LinkED_TCP();
            m_msg = new SreenPos();
            m_PreviousPoint.X = 0;
            m_PreviousPoint.Y = 0;
            Touch.FrameReported += Touch_FrameReported;

            DispatcherTimer tmr = new DispatcherTimer();
            tmr.Interval = TimeSpan.FromSeconds(1);
            tmr.Tick += OnTimerTick;
            tmr.Start();
        }

        void OnTimerTick(object sender, EventArgs e)
        {
            HideAllTrackPoint();
        }

        void Touch_FrameReported(object sender, TouchFrameEventArgs e)
        {
            
            if (this.mycanvas != null)
            {
                TouchPointCollection tpc = e.GetTouchPoints(this.mycanvas);
                TouchPoint otp = e.GetPrimaryTouchPoint(this.mycanvas);

                m_ScrollX = otp.Position.X - m_PreviousPoint.X;
                m_ScrollY = otp.Position.Y - m_PreviousPoint.Y;
                m_PreviousPoint.X = otp.Position.X;
                m_PreviousPoint.Y = otp.Position.Y;

                if (tpc.Count > 3)
                {
                    return;
                }

                m_CurrentFigerNumer = tpc.Count;

                int Count = 0;
                foreach (TouchPoint tp in tpc)
                {
                    if (tp.Position.X < mycanvas.RenderSize.Width && tp.Position.Y < mycanvas.RenderSize.Height)
                    {
                        SetTrackPointPos(++Count, new Point(tp.Position.X, tp.Position.Y));
                    }
                    
                }
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string ServerPort = "";

            if (NavigationContext.QueryString.TryGetValue("IP", out m_ServerIP)
                && NavigationContext.QueryString.TryGetValue("Port", out ServerPort))
            {
                m_ServerPort = Convert.ToInt32(ServerPort);

                if (m_TCP.Connect(m_ServerIP, m_ServerPort) == "Success"
                    && LinkED_DataSource.m_NtType == "TCP")
                {
                    m_bTCP = true;
                }
            }
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            m_UDP.Close();
            m_TCP.Close();
            Touch.FrameReported -= Touch_FrameReported;
            base.OnNavigatedFrom(e);
        }

        private void Border_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            e.Handled = true;
            base.OnManipulationStarted(e);
        }

        private void Border_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            ManipulationDelta delta = e.DeltaManipulation;
            System.Windows.Point pt = delta.Translation;

            // send mouse move message
            if (m_CurrentFigerNumer == 1)
            {
                m_msg.x = Convert.ToInt32(pt.X);
                m_msg.y = Convert.ToInt32(pt.Y);
                m_msg.msg_type = LinkED_MSG_TYPE.MOUSE_MOVE;
                SendMsg2Server(m_msg);
            }
            else if(m_CurrentFigerNumer == 2)// send mouse center scroll button
            {
                m_msg.x = Convert.ToInt32(-m_ScrollX);
                m_msg.y = Convert.ToInt32(-m_ScrollY);
                m_msg.msg_type = LinkED_MSG_TYPE.CENTER_SCROLL;
                SendMsg2Server(m_msg);
            }
            else // for extend
            {
                m_msg.x = Convert.ToInt32(m_ScrollX);
                m_msg.y = Convert.ToInt32(m_ScrollY);
                m_msg.msg_type = LinkED_MSG_TYPE.LEFT_DRAG;
                SendMsg2Server(m_msg);
            }

            e.Handled = true;
            base.OnManipulationDelta(e);
        }

        private void Border_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            HideAllTrackPoint();
            e.Handled = true;
            base.OnManipulationCompleted(e);
        }

        private void mytouchborder_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            m_msg.x = 0;
            m_msg.y = 0;
            m_msg.msg_type = LinkED_MSG_TYPE.LBTN_CLICKED;
            SendMsg2Server(m_msg);
        }

        private void mytouchborder_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            m_msg.x = 0;
            m_msg.y = 0;
            m_msg.msg_type = LinkED_MSG_TYPE.LBTN_DOUBLE_CLICKED;
            SendMsg2Server(m_msg);
        }

        private void mytouchborder_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            m_msg.x = 0;
            m_msg.y = 0;
            m_msg.msg_type = LinkED_MSG_TYPE.RBTN_CLICKED;
            SendMsg2Server(m_msg);
        }

        private void SendMsg2Server(SreenPos msg)
        {
            if (m_bTCP)
                SendMsgToServerTCP(msg);
            else
                SendMsgToServerUDP(msg);
        }

        private void SendMsgToServerUDP(SreenPos data)
        {
            if (m_UDP.Send(m_ServerIP, m_ServerPort, data) != "Success")
            {
                MessageBox.Show("网络连接错误，请确认计算机客户端是否已经打开，即将返回连接页面，请重连！");
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            }
        }

        private void SendMsgToServerTCP(SreenPos data)
        {
            if (m_TCP._socket.Connected)
            {
                m_TCP.Send(data);
            }
            else
            {
                MessageBox.Show("网络连接错误，请确认计算机客户端是否已经打开，即将返回连接页面，请重连！");
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            }
        }

        private void SetTrackPointPos(int numberOfTouchPoint, Point pos)
        {
            switch (numberOfTouchPoint)
            {
            case 1:
                trackpoint1.Visibility = Visibility.Visible;
                ellipseGeo1.Center = pos;
                break;
            case 2:
                trackpoint2.Visibility = Visibility.Visible;
                ellipseGeo2.Center = pos;
                break;
            case 3:
                trackpoint3.Visibility = Visibility.Visible;
                ellipseGeo3.Center = pos;
                break;
            }
        }

        private void HideAllTrackPoint()
        {
            trackpoint1.Visibility = Visibility.Collapsed;
            trackpoint2.Visibility = Visibility.Collapsed;
            trackpoint3.Visibility = Visibility.Collapsed;
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            if (btn1 == sender as Button)
            {
                m_msg.msg_type = LinkED_MSG_TYPE.OPEN_START_MENU;
                SendMsg2Server(m_msg);
            }

            if (btn2 == sender as Button)
            {
                m_msg.msg_type = LinkED_MSG_TYPE.SHOW_DESKTOP;
                SendMsg2Server(m_msg);
            }

            if (btn3 == sender as Button)
            {
                m_msg.msg_type = LinkED_MSG_TYPE.OPEN_COMPUTER;
                SendMsg2Server(m_msg);
            }

            if (btn4 == sender as Button)
            {
                PopupCotainer pc = new PopupCotainer(this);
                LinkED_POP_ShortCut pop = new LinkED_POP_ShortCut();
                pc.Show(pop);
            }
        }

        static public void SendShortCutMessage(LinkED_MSG_TYPE type)
        {
            m_msg.msg_type = type;

            if (m_bTCP)
            {
                if (m_TCP._socket.Connected)
                {
                    m_TCP.Send(m_msg);
                }
                else
                {
                    MessageBox.Show("网络连接错误，请确认计算机客户端是否已经打开, 请重连！");
                }
            }
            else
            {
                if (m_UDP.Send(m_ServerIP, m_ServerPort, m_msg) != "Success")
                {
                    MessageBox.Show("网络连接错误，请确认计算机客户端是否已经打开，即将返回连接页面，请重连！");
                }

            }
        }


    }
}


